package logic

import (
	"context"
	"unicode/utf8"
	"zhihu/zhihurpc/zhihurpcclient"

	"zhihu/api/internal/svc"
	"zhihu/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddAnswerLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddAnswerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddAnswerLogic {
	return &AddAnswerLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddAnswerLogic) AddAnswer(req *types.AddAnswerRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	get := utf8.RuneCountInString(req.Content)
	if get > 100 {
		return &types.Response{
			Message: "评论字数不能超过100字",
			Data:    nil,
			Code:    500,
		}, nil
	}

	ansewerAdd, err := l.svcCtx.ZhiHuRpc.AddAnswer(l.ctx, &zhihurpcclient.AddAnswerRequest{
		UserId:    req.UserId,
		ProblemId: req.ProblemId,
		Content:   req.Content,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}

	return &types.Response{
		Message: "问题回答成功",
		Data:    ansewerAdd,
		Code:    200,
	}, nil
}
